package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.controller.CxBase;
import com.ruoyi.system.domain.CooperativeMerchant;
import com.ruoyi.system.domain.PrizeLuck;
import com.ruoyi.system.mapper.CooperativeMerchantMapper;
import com.ruoyi.system.mapper.PrizeLuckMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PrizeInfoMapper;
import com.ruoyi.system.domain.PrizeInfo;
import com.ruoyi.system.service.IPrizeInfoService;

/**
 * 奖品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-07
 */
@Service
public class PrizeInfoServiceImpl extends CxBase implements IPrizeInfoService
{
    @Autowired
    private PrizeInfoMapper prizeInfoMapper;
    @Autowired
    private CooperativeMerchantMapper cooperativeMerchantMapper;
    @Autowired
    private PrizeLuckMapper prizeLuckMapper;

    /**
     * 查询奖品
     * 
     * @param prizeInfo 奖品ID
     * @return 奖品
     */
    @Override
    public PrizeInfo selectPrizeInfoById(PrizeInfo prizeInfo)
    {
        startPage();
        float sum = 0;
        if(!checkNull(prizeInfo.getPrizeSurplusProbability())){
            PrizeLuck prizeLuck = new PrizeLuck();
            prizeLuck.setSafetyExaminationId(prizeInfo.getMerchantId());
            List<PrizeLuck> prizeLucks = prizeLuckMapper.selectPrizeLuckList(prizeLuck);
            for(PrizeLuck prizeLuck1:prizeLucks){
                sum+=prizeLuck1.getPrizeProbability();
            }
            sum = (float)Math.round((1-sum)*1000)/1000;
        }
        prizeInfo = prizeInfoMapper.selectPrizeInfoById(prizeInfo.getPrizeId());
        prizeInfo.setPrizeSurplusProbability(sum);
        return prizeInfo;
    }

    /**
     * 查询奖品列表
     * 
     * @param prizeInfo 奖品
     * @return 奖品
     */
    @Override
    public List<PrizeInfo> selectPrizeInfoList(PrizeInfo prizeInfo)
    {
        startPage();
        return prizeInfoMapper.selectPrizeInfoList(prizeInfo);
    }

    /**
     * 新增奖品
     * 
     * @param prizeInfo 奖品
     * @return 结果
     */
    @Override
    public int insertPrizeInfo(PrizeInfo prizeInfo)
    {
        if(!checkNull(prizeInfo.getMerchantId())&&!checkNull(prizeInfo.getPrizeName())&&!checkNull(prizeInfo.getPrizeAddress())&&!checkNull(prizeInfo.getPrizeAmount())&&!checkNull(prizeInfo.getPrizePic())){
            CooperativeMerchant cooperativeMerchant = new CooperativeMerchant();
            cooperativeMerchant.setMerchantId(prizeInfo.getMerchantId());
            List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
            if(cooperativeMerchants.size()>0) {
                prizeInfo.setMerchantName(cooperativeMerchants.get(0).getMerchantName());
                prizeInfo.setPrizeId(getStamp().toString());
                return prizeInfoMapper.insertPrizeInfo(prizeInfo);
            }
        }
        return 0;
    }

    /**
     * 修改奖品
     * 
     * @param prizeInfo 奖品
     * @return 结果
     */
    @Override
    public int updatePrizeInfo(PrizeInfo prizeInfo)
    {
        if(!checkNull(prizeInfo.getPrizeId())){
            if(!checkNull(prizeInfo.getMerchantId())) {
                CooperativeMerchant cooperativeMerchant = new CooperativeMerchant();
                cooperativeMerchant.setMerchantId(prizeInfo.getMerchantId());
                List<CooperativeMerchant> cooperativeMerchants = cooperativeMerchantMapper.selectCooperativeMerchantList(cooperativeMerchant);
                if (cooperativeMerchants.size() <1 ) {
                    return 0;
                }
                prizeInfo.setMerchantName(cooperativeMerchants.get(0).getMerchantName());
            }
            return prizeInfoMapper.updatePrizeInfo(prizeInfo);
        }
        return 0;
    }

    /**
     * 批量删除奖品
     * 
     * @param prizeIds 需要删除的奖品ID
     * @return 结果
     */
    @Override
    public int deletePrizeInfoByIds(String[] prizeIds)
    {
        if(prizeIds.length>0){
            return prizeInfoMapper.deletePrizeInfoByIds(prizeIds);
        }
        return 0;
    }

    /**
     * 删除奖品信息
     * 
     * @param prizeId 奖品ID
     * @return 结果
     */
    @Override
    public int deletePrizeInfoById(String prizeId)
    {
        return prizeInfoMapper.deletePrizeInfoById(prizeId);
    }
}
